#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>

/////////////////////////////////////////////////////
void download()
{
    printf("this is a download task is running!\n");
}
void printLog()
{
    printf("this is a write log task is running!\n");
}
void show()
{
    printf("this is a show task is running!\n");
}
//
void worker(int n)
{
    printf("I am a child process,pid:%d,cnt:%d\n",getpid(),n);
}
int main()
{
    pid_t id=fork();
    if(id==0)
    {
        //子进程
        int cnt=10;
        while(cnt)
        {
            worker(cnt);
            sleep(2);
            cnt--;
        }
        exit(0);
    }

    //父进程
    while(1)
    {    
        int status=0;
        pid_t rid=waitpid(id,&status,WNOHANG);//WNOHANG：非阻塞式等待
        if(rid>0)
        {
            printf("等待成功，子进程成功退出。退出码：%d,退出信号：%d\n",(status>>8)&0xFF,status&0x7F);
            break;
        }
        else if(rid==0)
        {
            printf("等待成功，但是子进程未退出，继续等待。即非阻塞式等待成功\n");
        }
        else
        {
            printf("等待失败\n");
            break;
        }
        sleep(1);
    }
    return 0;
}
